package com.ev.hr.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.apis.model.DsResultResponse;
import com.ev.common.enums.AuditStatusDict;
import com.ev.framework.config.Constant;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.*;
import com.ev.hr.dao.SalaryAccountingDao;
import com.ev.hr.domain.*;
import com.ev.hr.enums.AttendanceProjectDict;
import com.ev.hr.enums.EmployeeStateDict;
import com.ev.hr.enums.EnumFormulasParameterType;
import com.ev.hr.enums.SalaryProjectDict;
import com.ev.hr.service.*;
import com.ev.hr.vo.SalaryAccountingPageParam;
import com.ev.hr.vo.SalaryAccountingVO;
import com.ev.system.service.RoleService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SalaryAccountingServiceImpl implements SalaryAccountingService {
    @Autowired
    private SalaryAccountingDao salaryAccountingDao;
    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private SalaryArchivesService salaryArchivesService;
    @Autowired
    private SalaryFormulaChildService salaryFormulaChildService;
    @Autowired
    private AttendanceRecordService attendanceRecordService;
    @Autowired
    private SalaryProjectService salaryProjectService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MessageSourceHandler messageSourceHandler;

    @Override
    public SalaryAccountingDO get(Long id) {
        return salaryAccountingDao.get(id);
    }

    @Override
    public List<SalaryAccountingDO> list(Map<String, Object> map) {
        return salaryAccountingDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return salaryAccountingDao.count(map);
    }

    @Override
    public int save(SalaryAccountingDO salaryAccounting) {
        return salaryAccountingDao.save(salaryAccounting);
    }

    @Override
    public int update(SalaryAccountingDO salaryAccounting) {
        return salaryAccountingDao.update(salaryAccounting);
    }

    @Override
    public int updateAll(SalaryAccountingDO salaryAccounting) {
        return salaryAccountingDao.updateAll(salaryAccounting);
    }

    @Override
    public int remove(Long id) {
        return salaryAccountingDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return salaryAccountingDao.batchRemove(ids);
    }

    @Override
    public R saveAndVerify(List<SalaryAccountingVO> saveParam) {
        if (saveParam.size() > 0) {
            Map<String, Object> param = Maps.newHashMap();
            // 找出需要薪资核算显示的所有薪资项目
            param.put("isShowSalaryAccounting", 1);
            List<SalaryProjectDO> salaryProjectDOS = salaryProjectService.list(param);
            Long fixed = SalaryProjectDict.FIXED.getId();
            Long change = SalaryProjectDict.CHANGE.getId();
            List<String> fixedProject = salaryProjectDOS
                    .stream()
                    .filter(e -> fixed.equals(e.getAttribute()))
                    .map(SalaryProjectDO::getValue)
                    .collect(Collectors.toList());
            List<String> changeProject = salaryProjectDOS
                    .stream()
                    .filter(e -> change.equals(e.getAttribute()))
                    .map(SalaryProjectDO::getValue)
                    .collect(Collectors.toList());
            List<SalaryAccountingDO> salaryAccountingDOS = Lists.newArrayList();
            SalaryAccountingDO salaryAccountingDO;
            JSONArray jsonArray;
            for (SalaryAccountingVO accountingVO : saveParam) {
                salaryAccountingDO = new SalaryAccountingDO();
                salaryAccountingDO.setId(accountingVO.getId());
                String salaryProjectJson = accountingVO.getSalaryProjectJson();
                jsonArray = JSON.parseArray(salaryProjectJson);
                // 固定项目
                List<JSONObject> fixedProjectJson = jsonArray
                        .stream()
                        .map(e -> (JSONObject) e)
                        .filter(e -> fixedProject.contains(e.getString("property")))
                        .collect(Collectors.toList());
                // 变动项目
                List<JSONObject> changeProjectJson = jsonArray
                        .stream()
                        .map(e -> (JSONObject) e)
                        .filter(e -> changeProject.contains(e.getString("property")))
                        .collect(Collectors.toList());
                if (fixedProjectJson.size() > 0) {
                    salaryAccountingDO.setSalaryFixedJson(JSON.toJSONString(fixedProjectJson));
                }
                if (changeProjectJson.size() > 0) {
                    salaryAccountingDO.setSalaryChangeJson(JSON.toJSONString(changeProjectJson));
                }
                salaryAccountingDOS.add(salaryAccountingDO);
            }

            // 批量修改项目
//			for (SalaryAccountingDO accountingDO : salaryAccountingDOS) {
//				salaryAccountingDao.update(accountingDO);
//			}
            salaryAccountingDao.batchUpdate(salaryAccountingDOS);
            return R.ok();
        }
        return R.error();
    }

    @Override
    public Map<String, Object> getById(Long id) {
        SalaryAccountingDO salaryAccounting = salaryAccountingDao.get(id);
        if (salaryAccounting != null) {
            Map<String, Object> result = Maps.newHashMap();
            result.put("data", salaryAccounting);
            return R.ok(result);
        }
        return R.error();
    }

    @Override
    public Map<String, Object> pageList(SalaryAccountingPageParam pageParam) {
        Integer pageSize = pageParam.getPagesize();
        Integer pageNo = pageParam.getPageno();
        pageParam.setPageno(1);
        pageParam.setPagesize(Integer.MAX_VALUE);
        Map<String, Object> result = Maps.newHashMap();
        if(pageParam.getMonthDate()!=null){
            pageParam.setMonthDate(pageParam.getMonthDate().substring(0,7));
        }
        pageParam.setEmployeeName(StringUtils.sqlLike(pageParam.getEmployeeName()));

        JSONObject o = (JSONObject) JSONObject.toJSON(pageParam);
        // 若个人薪资则不调用数据权限
        if (pageParam.getEmployeeId() == null){
            o.putAll(roleService.getDataPermission(3));
        }

        List<Map<String, Object>> data = salaryAccountingDao.pageListByMap(o);
        if (data.size() > 0) {
            List<JSONObject> jsonObjectList = Lists.newArrayList();
            data.forEach(e -> {
                Object salaryFixedJson = e.get("salaryFixedJson");
                if (salaryFixedJson != null) {
                    String salaryFixedJsonToString = salaryFixedJson.toString();
                    JSONArray salaryFixedJsonArray = JSON.parseArray(salaryFixedJsonToString);
                    salaryFixedJsonArray.forEach(jo -> {
                        JSONObject jsonObject = (JSONObject) jo;
                        jsonObjectList.add(jsonObject);
                        e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                    });
                }
                Object salaryChangeJson = e.get("salaryChangeJson");
                if (salaryChangeJson != null) {
                    String salaryChangeJsonToString = salaryChangeJson.toString();
                    JSONArray salaryChangeJsonArray = JSON.parseArray(salaryChangeJsonToString);

                    salaryChangeJsonArray.forEach(jo -> {
                        JSONObject jsonObject = (JSONObject) jo;
                        jsonObjectList.add(jsonObject);
                        e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                    });
                }
            });
            Map<String, BigDecimal> collect = jsonObjectList
                    .stream()
                    .collect(Collectors.toMap(k -> k.getString("property")
                            , v -> BigDecimal.valueOf(v.getDoubleValue("amount"))
                            , BigDecimal::add));


            int total = data.size();
            List<Map<String, Object>> startPage = PageUtils.startPage(data, pageNo, pageSize);

            result.put("total",collect);

            result.put("data", new DsResultResponse(pageNo, pageSize, total, startPage));
        }
        return R.ok(result);
    }

    @Override
    public R getSalaryAccountingData(SalaryAccountingPageParam pageParam) {
        pageParam.setPagesize(Integer.MAX_VALUE);
        String monthDate = pageParam.getMonthDate();
        pageParam.setMonthDate(monthDate.substring(0, 7));
        Date dateNow = DateFormatUtil.getDateByParttern(monthDate, DateFormatUtil.DATE_PATTERN_YMD);
        if(dateNow == null){
            return R.error(messageSourceHandler.getMessage("scm.stock.timeIsStart",null));
        }
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)-1);
        if(instance.getTime().before(dateNow)){
            return R.error(messageSourceHandler.getMessage("scm.stock.time",null));
        }


        List<Map<String, Object>> pageList = salaryAccountingDao.pageList(pageParam);
        if (pageList.size() == 0) {
            generateAccounting(dateNow,monthDate);
            pageList = salaryAccountingDao.pageList(pageParam);
        }
        if (pageList.size() == 0) {
            return R.ok();
        }
        pageList.forEach(e -> {
            Object salaryFixedJson = e.get("salaryFixedJson");
            if (salaryFixedJson != null) {
                String salaryFixedJsonToString = salaryFixedJson.toString();
                JSONArray salaryFixedJsonArray = JSON.parseArray(salaryFixedJsonToString);
                salaryFixedJsonArray.forEach(jo -> {
                    JSONObject jsonObject = (JSONObject) jo;
                    e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                });
            }
            Object salaryChangeJson = e.get("salaryChangeJson");
            if (salaryChangeJson != null && StringUtils.isNoneEmpty(salaryChangeJson.toString())) {
                String salaryChangeJsonToString = salaryChangeJson.toString();
                JSONArray salaryChangeJsonArray = JSON.parseArray(salaryChangeJsonToString);

                salaryChangeJsonArray.forEach(jo -> {
                    JSONObject jsonObject = (JSONObject) jo;
                    e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                });
            }
        });
        Map<String, Object> result = Maps.newHashMap();
        result.put("data", pageList);
        return R.ok(result);
    }

    @Override
    public R compute(SalaryAccountingPageParam param) {
        param.setPagesize(Integer.MAX_VALUE);
        String monthDate = param.getMonthDate();
        param.setMonthDate(monthDate.substring(0, 7));

        // 获取计算公式
        Map<String, Object> params = Maps.newHashMap();
        // 找出所有薪资项目
        List<SalaryProjectDO> salaryProjectDOS = salaryProjectService.list(params);
        Long fixedId = SalaryProjectDict.FIXED.getId();
        // 找出固定薪资项目
        List<Long> fixedProjectList = salaryProjectDOS
                .stream()
                .filter(e -> Objects.equals(fixedId, e.getAttribute()))
                .map(SalaryProjectDO::getId)
                .collect(Collectors.toList());

        List<Long> otherIdList = salaryProjectDOS
                .stream()
                .map(SalaryProjectDO::getId)
                .collect(Collectors.toList());
        otherIdList.removeAll(fixedProjectList);
        
        //  value  对应 id的map
        Map<String, Long> projectValueToId = salaryProjectDOS
                .stream()
                .collect(Collectors.toMap( SalaryProjectDO::getValue,SalaryProjectDO::getId));
        //  id  对应 value的map
        Map<Long, String> projectIdToValue = salaryProjectDOS
                .stream()
                .collect(Collectors.toMap( SalaryProjectDO::getId,SalaryProjectDO::getValue));

        otherIdList.removeAll(fixedProjectList);

        params.put("mainId", param.getFormulaId());
        List<SalaryFormulaChildDO> formulaChildDOS = salaryFormulaChildService.list(params);
        if (formulaChildDOS.size() == 0) {
            return R.error(messageSourceHandler.getMessage("hr.salary.formula.isEmpty", null));
        }

        formulaChildDOS.sort(Comparator.comparing(SalaryFormulaChildDO::getLevel));
        // 获取最后一个公式
        SalaryFormulaChildDO salaryFormulaChildDO = formulaChildDOS.get(formulaChildDOS.size() - 1);
        String formulaStr = salaryFormulaChildDO.getFormulaStr();
        Long formulaId = salaryFormulaChildDO.getMainId();
        String salaryProjectCode = EnumFormulasParameterType.SALARY_PROJECT.getCode();
        // 获取薪资项目是固定项目的
        List<Long> fixedProjectIdList = Lists.newArrayList();
        // 需要计算的项目=前
        LinkedHashMap<Long, List<String>> computeProjectMap = new LinkedHashMap<>();
        List<String> computeProjectValueList;
        for (SalaryFormulaChildDO formulaChildDO : formulaChildDOS) {
            // 要计算项目为固定项
            Long projectId = formulaChildDO.getProjectId();
            if (fixedProjectList.contains(projectId)) {
                fixedProjectIdList.add(projectId);
            }
            // 需要计算的项目=后
            computeProjectValueList = Lists.newArrayList();
            String formulaJsonarray = formulaChildDO.getFormulaJsonarray();
            JSONArray jsonArray = JSON.parseArray(formulaJsonarray);
            for (Object o : jsonArray) {
                JSONObject jsonObject = (JSONObject) o;
                String type = jsonObject.getString("type");
                if (Objects.equals(type, salaryProjectCode)) {
                    computeProjectValueList.add(jsonObject.getString("id"));
                }
            }
            computeProjectMap.put(projectId, computeProjectValueList);
        }

        // 如果有提示公式被计算薪资项目有固定项目存在，并列表提示相应的薪资项目
        if (fixedProjectIdList.size() > 0) {
            String[] args = {StringUtils.join(salaryProjectDOS
                    .stream()
                    .filter(e -> fixedProjectIdList.contains(e.getId()))
                    .map(SalaryProjectDO::getSalaryItemName).toArray(), ",")};
            return R.error(messageSourceHandler.getMessage("hr.salary.project.isFixed", args));
        }
        // 检查公式数据是否有来源

        // 找出所有考勤项目
        List<String> attendanceProject = AttendanceProjectDict.getListValue();
        Map<String,Object> attendanceParam = Maps.newHashMap();
        // 考勤数据
        attendanceParam.put("period",param.getMonthDate());
        List<Map<String, Object>> attendanceList = attendanceRecordService.getList(attendanceParam);
        Map<String, Map<String, Object>> employeeToAttendance = attendanceList
                .stream()
                .collect(Collectors.toMap(k -> k.get("employee_id").toString(), v -> v));


        List<Long> upProjectList = Lists.newArrayList();
        int index = 0;
        for (Map.Entry<Long, List<String>> entry : computeProjectMap.entrySet()) {
            index++;
            // 若是最后一个项目则也跳过
            if(index == computeProjectMap.size()){
                continue;
            }
            List<String> projectList = entry.getValue();
            for (String project : projectList) {
                // 若是固定项目也跳过
                // 若是上面出现过的也跳过
                // 若是考勤项目也跳过
                if (    attendanceProject.contains(project)
                        ||fixedProjectList.contains(projectValueToId.get(project))
                        || upProjectList.contains(projectValueToId.get(project))) {
                    continue;
                }
                Long errorProject = entry.getKey();
                String[] args = {salaryProjectDOS
                        .stream()
                        .filter(e -> Objects.equals(e.getId(), errorProject))
                        .findFirst()
                        .orElse(new SalaryProjectDO())
                        .getSalaryItemName()};
                return R.error(messageSourceHandler.getMessage("hr.salary.project.unCompute", args));
            }
            upProjectList.add(entry.getKey());
        }

        // 开始计算
        List<Map<String, Object>> pageList = salaryAccountingDao.pageList(param);
        pageList.forEach(e -> {
            Object salaryFixedJson = e.get("salaryFixedJson");
            if (salaryFixedJson != null) {
                String salaryFixedJsonToString = salaryFixedJson.toString();
                JSONArray salaryFixedJsonArray = JSON.parseArray(salaryFixedJsonToString);
                salaryFixedJsonArray.forEach(jo -> {
                    JSONObject jsonObject = (JSONObject) jo;
                    e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                });
            }
            Object salaryChangeJson = e.get("salaryChangeJson");
            if (salaryChangeJson != null) {
                String salaryChangeJsonToString = salaryChangeJson.toString();
                JSONArray salaryChangeJsonArray = JSON.parseArray(salaryChangeJsonToString);

                salaryChangeJsonArray.forEach(jo -> {
                    JSONObject jsonObject = (JSONObject) jo;
                    e.put(jsonObject.getString("property"), jsonObject.getDoubleValue("amount"));
                });
            }
        });
        Map<Long, FormulaUtil.Result> projectAmount = Maps.newHashMap();
        for (Map<String, Object> account : pageList) {
            // 计算公式
            for (SalaryFormulaChildDO formulaChildDO : formulaChildDOS) {
                StringBuilder convertFormula = new StringBuilder();
                String formulaJsonarray = formulaChildDO.getFormulaJsonarray();
                JSONArray formulaJsonArray = JSON.parseArray(formulaJsonarray);
                for (Object o : formulaJsonArray) {
                    JSONObject jsonObject = (JSONObject) o;
                    String type = jsonObject.getString("type");
                    // 若是薪资项目
                    if (Objects.equals(type, salaryProjectCode)) {
                        String value = jsonObject.getString("id");
                        // 若是考勤项目则取考勤项目内的值
                        if(attendanceProject.contains(value)){
                            Map<String, Object> singleEmployeeToAttendance = employeeToAttendance.get(account.get("employeeId").toString());

                            if(singleEmployeeToAttendance==null){
                                // 若无次人的考勤记录 则该项目记录为0
                                convertFormula.append("0");
                            }else {
                                // 若有考勤记录则取其考勤值
                                convertFormula.append(singleEmployeeToAttendance.getOrDefault(value,"0"));
                            }
                            continue;
                        }

                        Long projectId = projectValueToId.get(value);
                        // 若是已经计算过的则取计算过的值
                        if (projectAmount.containsKey(projectId)) {
                            convertFormula.append(projectAmount.get(projectId).getValue());
                        } else {
                            convertFormula.append(account.get(jsonObject.getString("id")));
                        }
                    } else {
                        // 若是运算符 数字 括号 则直接拼接
                        convertFormula.append(jsonObject.getString("id"));
                    }
                }
                FormulaUtil.Result result = FormulaUtil.iFunction(convertFormula.toString());
                Long projectId = formulaChildDO.getProjectId();
                projectAmount.put(projectId, result);
                account.put(projectIdToValue.get(projectId), result.getValue());
            }
        }
        List<SalaryAccountingDO> salaryAccountingDOS = Lists.newArrayList();

        SalaryAccountingDO salaryAccountingDO;
        List<Map<String, Object>> fixedList;
        List<Map<String, Object>> changeAndOtherList;
        Map<String,Object> map;
        for (Map<String, Object> account : pageList) {
            fixedList = Lists.newArrayList();
            changeAndOtherList = Lists.newArrayList();
            salaryAccountingDO = new SalaryAccountingDO();
            salaryAccountingDO.setId(Long.parseLong(account.get("id").toString()));
            for (String s : account.keySet()) {
                map = Maps.newHashMap();
                if (projectValueToId.containsKey(s)) {
                    if (fixedProjectIdList.contains(projectValueToId.get(s))) {
                        map.put("property",s);
                        map.put("amount",account.get(s));
                        fixedList.add(map);
                        continue;
                    }
                    if (otherIdList.contains(projectValueToId.get(s))) {
                        map.put("property",s);
                        map.put("amount",account.get(s));
                        changeAndOtherList.add(map);
                    }
                }

            }
            if (changeAndOtherList.size() > 0) {
                salaryAccountingDO.setSalaryChangeJson(JSON.toJSONString(changeAndOtherList));
            }
            if (fixedList.size() > 0) {
                salaryAccountingDO.setSalaryFixedJson(JSON.toJSONString(fixedList));
            }
            salaryAccountingDO.setFormulaId(formulaId);
            salaryAccountingDO.setFormulaStr(formulaStr);
            salaryAccountingDOS.add(salaryAccountingDO);
        }

        // 批量修改项目
        salaryAccountingDao.batchUpdate(salaryAccountingDOS);
        return R.ok();
    }

    @Override
    public R audit(SalaryAccountingPageParam param, boolean isAudit) {
        param.setMonthDate(param.getMonthDate().substring(0, 7));
        param.setPagesize(Integer.MAX_VALUE);
        List<Map<String, Object>> accountingList = salaryAccountingDao.pageList(param);
        if (accountingList.size() == 0) {
            return R.error(messageSourceHandler.getMessage("hr.salary.audit.isEmpty", null));
        }

        long auditSign = Long.parseLong(accountingList.get(0).get("auditSign").toString());

        List<Long> idList = accountingList
                .stream()
                .map(e -> Long.parseLong(e.get("id").toString()))
                .collect(Collectors.toList());
        List<SalaryAccountingDO> salaryAccountingDOList = Lists.newArrayList();
        long okAuditId = AuditStatusDict.OK_AUDIT.getId();
        long waitAuditId = AuditStatusDict.WAIT_AUDIT.getId();
        if (isAudit) {
            if (okAuditId == auditSign) {
                return R.error(messageSourceHandler.getMessage("common.duplicate.approved", null));
            }
        } else {
            if (waitAuditId == auditSign) {
                return R.error(messageSourceHandler.getMessage("common.massge.okWaitAudit", null));
            }
        }
        idList.forEach(id -> {
            SalaryAccountingDO salaryAccountingDO = new SalaryAccountingDO();
            salaryAccountingDO.setId(id);
            salaryAccountingDO.setAuditSign(isAudit ? okAuditId : waitAuditId);
            salaryAccountingDOList.add(salaryAccountingDO);
        });
        this.salaryAccountingDao.batchUpdate(salaryAccountingDOList);
        return R.ok();
    }

    /**
     * 根据所属时期生成薪资核算数据
     *
     */
    public void generateAccounting(Date dateNow,String monthDate) {
        //获取符合条件的员工
        // 按照日期来 例如本月2020-05  则将员工入职为2020-06之前的员工找出
        Calendar nextMonth = Calendar.getInstance();
        nextMonth.setTime(dateNow);
        nextMonth.set(Calendar.DAY_OF_MONTH, 1);
        nextMonth.set(Calendar.MONTH, nextMonth.get(Calendar.MONTH) + 1);
        Map<String, Object> param = Maps.newHashMap();
        param.put("nextMonth",DateFormatUtil.getFormateDate(nextMonth.getTime()));
        List<Long> stateList = Lists.newArrayList();
        stateList.add(EmployeeStateDict.WORK.getId());
        stateList.add(EmployeeStateDict.PROBATION.getId());
        param.put("stateList",stateList);

        List<EmployeeInfoDO> employeeList = employeeInfoService.list(param);
        if (employeeList.size() == 0) {
            return;
        }

        List<Long> employeeIdList = employeeList
                .stream()
                .map(EmployeeInfoDO::getId)
                .collect(Collectors.toList());
        // 找出计算月刚入职的员工
        String monthDateSub = monthDate.substring(0, 7);
        Map<Long, Boolean> isThisMonthMap = employeeList
                .stream()
                .collect(Collectors.toMap(EmployeeInfoDO::getId, v -> {
                    String formateDate = DateFormatUtil.getFormateDate(v.getEntryDate());
                    if (StringUtils.isEmpty(formateDate)) {
                        return true;
                    }
                    return formateDate.startsWith(monthDateSub);
                }));

        param.put("employeeIdList", employeeIdList);
        param.put("month", monthDate);
        //根据员工获取薪资档案
        List<SalaryArchivesDO> salaryArchivesList = salaryArchivesService.list(param);
        List<SalaryAccountingDO> accountingList = Lists.newArrayList();
        long waitAuditId = AuditStatusDict.WAIT_AUDIT.getId();
        if (salaryArchivesList.size() != employeeIdList.size()) {
            List<Long> lastId = salaryArchivesList
                    .stream()
                    .map(SalaryArchivesDO::getEmployeeId)
                    .collect(Collectors.toList());
            employeeList.removeIf(e -> lastId.contains(e.getId()));
            // 增加计算月无薪资档案的
            employeeList.forEach(employeeInfoDO -> {
                SalaryAccountingDO salaryAccounting = new SalaryAccountingDO();
                salaryAccounting.setEmployeeId(employeeInfoDO.getId());
                salaryAccounting.setDeptId(employeeInfoDO.getDeptId());
                salaryAccounting.setAuditSign(waitAuditId);
                salaryAccounting.setMonthDate(dateNow);
                accountingList.add(salaryAccounting);
            });
        }
        // 增加计算月有薪资档案的
        salaryArchivesList.forEach(salaryArchives -> {
            SalaryAccountingDO salaryAccounting = new SalaryAccountingDO();
            salaryAccounting.setEmployeeId(salaryArchives.getEmployeeId());
            salaryAccounting.setDeptId(salaryArchives.getDeptId());
            salaryAccounting.setAuditSign(Constant.WAIT_AUDIT);
            salaryAccounting.setMonthDate(dateNow);
            salaryAccounting.setSalaryFixedJson(salaryArchives.getSalaryProjectJson());
            accountingList.add(salaryAccounting);
        });
        // 将本月刚入职的标记出来
        accountingList.forEach(e->{
            Long employeeId = e.getEmployeeId();
            e.setIsThisMonth(isThisMonthMap.get(employeeId)?1:0);
        });

        this.salaryAccountingDao.batchSave(accountingList);

    }

}
